// pages/home/home-nav/personal-overview/bpl/add-bpl/index.ts
import Notify from '@vant/weapp/notify/notify';
import { addBPLRecord } from '../../../../../../utils/api/BPLControl'
import userBaseInfo from '../../../../../../stores/userBaseInfo'
import { mySleep } from '../../../../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateValue: '请选择记录的日期',
    timeValue: '请选择记录的时间',
    user: {
      SBP: 0,
      DBP: 0,
      HR: 0,
    },
    popupShow: false,
    dateShow: false,
    currentDate: new Date().getTime(),
    minDate: new Date(2000, 0, 1).getTime(),
    maxDate: new Date().getTime(),
    formatter(type: string, value: any) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    timeShow: false,
    currentTime: '',
    minHour: 0,
    maxHour: 24,
  },

  popupClose() {
    this.setData({
      popupShow: false,
      dateShow: false,
      timeShow: false,
    });
  },

  letDateShow() {
    this.setData({
      popupShow: true,
      dateShow: true,
    })
  },

  dateClose() {
    this.setData({
      dateShow: false,
      popupShow: false,
    });
  },

  onInputDate(event: any) {
    this.setData({
      currentDate: event.detail,
    });
  },

  onConfirmDate(event: any) {
    console.log(event);
    let timeStamp = event.detail;
    let date = new Date(timeStamp);
    const formattedDate = date.getFullYear() + '-' +
      String(date.getMonth() + 1).padStart(2, '0') + '-' +
      String(date.getDate()).padStart(2, '0') as string;

    console.log(formattedDate);
    this.setData({
      dateShow: false,
      popupShow: false,
      dateValue: formattedDate,
    })
  },

  letTimeShow() {
    this.setData({
      popupShow: true,
      timeShow: true,
    })
  },

  timeClose() {
    this.setData({
      timeShow: false,
      popupShow: false,
    });
  },

  onInputTime(event: any) {
    this.setData({
      currentTime: event.detail,
    });
  },

  onConfirmTime(event: any) {
    console.log(event);
    this.setData({
      timeShow: false,
      popupShow: false,
      timeValue: event.detail,
    })
  },

  inputSBP(e: any) {
    let SBP = e.detail.value;
    this.setData({
      'user.SBP': SBP,
    })
  },

  inputDBP(e: any) {
    let DBP = e.detail.value;
    this.setData({
      'user.DBP': DBP,
    })
  },

  inputHR(e: any) {
    let HR = e.detail.value;
    this.setData({
      'user.HR': HR,
    })
  },

  addBPL() {
    if (this.data.dateValue === '请选择记录的日期') {
      Notify({ type: 'danger', message: '请添加记录日期' });
      return;
    }
    if (this.data.timeValue === '请选择记录的时间') {
      Notify({ type: 'danger', message: '请添加记录时间' });
      return;
    }
    if (this.data.user.SBP < 90 || this.data.user.SBP > 300) {
      Notify({ type: 'danger', message: '请检查高压（收缩压）输入数据是否正确' });
      return;
    }
    if (this.data.user.DBP < 60 || this.data.user.DBP > 200) {
      Notify({ type: 'danger', message: '请检查低压（舒张压）输入数据是否正确' });
      return;
    }
    if (this.data.user.HR < 60 || this.data.user.HR > 200) {
      Notify({ type: 'danger', message: '请检查心率输入数据是否正确' });
      return;
    }
    let pathData = this.data.dateValue + ' ' + this.data.timeValue + ':00' as string;
    const data = {
      systolic: this.data.user.SBP,
      diastolic: this.data.user.DBP,
      heartRate: this.data.user.HR,
      uploadTime: pathData,
      patientUid: userBaseInfo.data.patientUid,
    }
    addBPLRecord(data).then(async (res: any) => {
      console.log('BPL提交成功', res);
      await mySleep(500)
      wx.navigateBack({
        delta: 1
      })
    }).catch((err: any) => {
      console.log('BPL提交失败', err);
      Notify({ type: 'danger', message: '当前提交失败，请稍后再试' });
    })
  },

  loadData() {
    let now = new Date();
    let minDate = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate());
    let currentTime = now.getHours() + ':' + now.getMinutes() as string;
    console.log(currentTime);
    this.setData({
      minDate: minDate.getTime(),
      currentTime: currentTime,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})